﻿using Blog.Core.Domain.Dictionary;
using Blog.Core.DTO.ViewModel;
using Blog.Core.DTO;
using Blog.Core.IRepositories;
using Blog.Core.IRepositories.Dictionary;
using Blog.Core.IServices.Dictionary;
using Blog.Core.Repositories;
using Blog.Core.Repositories.Dictionary;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Blog.Core.DTO.ViewModel.Dictionary;

namespace Blog.Core.Services.Dictionary
{
    /// <summary>
    /// 字典数据表的服务层
    /// </summary>
    public class DictDataService : BaseService<DictData>, IDictDataService
    {
        private readonly IDictDataRepository dictDataRepository;
        private readonly IDictTypeRepository dictTypeRepository;
        private readonly IUnOfWork unOfWork;
        public DictDataService(IRepository<DictData> repository,IDictDataRepository dictDataRepository, IDictTypeRepository dictTypeRepository, IUnOfWork unOfWork) : base(repository, unOfWork)
        {
            this.dictDataRepository = dictDataRepository;
            this.dictTypeRepository = dictTypeRepository;
            this.unOfWork = unOfWork;
        }
        /// <summary>
        /// 字典信息列表
        /// </summary>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public async Task<ResponseResultDTO<PageDTO<GetDictDataDTO>>> GetDictData(int pageindex, int pagesize, string name)
        {
            var list = from x in await dictDataRepository.GetAll()
                       join y in await dictTypeRepository.GetAll() on x.Dict_type equals y.Dict_id
                       select new GetDictDataDTO
                       {
                           Dict_code = x.Dict_code,
                           Dict_value = x.Dict_value,
                           Dict_label = x.Dict_label,
                           Dict_sort = x.Dict_sort,
                           Dict_class = x.Dict_class,
                           Dict_type = x.Dict_type,
                           Is_default = x.Is_default,
                           List_class = x.List_class,
                           Status = x.Status,
                           TypeName = y.Dict_type,
                           CreateName = x.CreateName,
                           CreateTime = x.CreateTime,
                           Delete = x.Delete,
                           UpdateName = x.UpdateName,
                           UpdateTime = x.UpdateTime,
                       };
            if (!string.IsNullOrEmpty(name))
            {
                list = list.Where(m => m.Dict_value.Contains(name));
            }
            PageDTO<GetDictDataDTO> pageDTO = new PageDTO<GetDictDataDTO>()
            {
                RecordCount = list.Count(),
                Plist = list.OrderBy(m => m.Dict_code).Skip((pageindex - 1) * pagesize).Take(pagesize).ToList(),
            };
            return new ResponseResultDTO<PageDTO<GetDictDataDTO>>
            {
                Code = ResultStatusCode.SUCCESS,
                Data = pageDTO,
                Message = "查询成功！",
            };
        }
    }
}
